//
//  HistoryView.swift
//  HisPaste
//
//  Created by 烟雀 on 2024/2/16.
//

import Foundation
import SwiftUI

struct HistoryView: View{
    
    @ObservedObject
    var pasteRecord = PasteRecord.this
    @State
    var selectedRow: Int = 0
    
    let itemLength: Int = 30
    
    var body: some View{
        VStack {
            Text("总条目数: \(pasteRecord.records.inCache_.count)")
            List(selection: $selectedRow, content: {
                ForEach(pasteRecord.records.inCache_, id: \.self, content: {
                    item in
                    
                    HStack{
                        
                        ItemView(data: item)
//                        Text(item.count > itemLength ? "\(item.prefix(30)) ..." : item)
                        //                    右键菜单与单击不能共存
                        //                        .contextMenu(
                        //                            ContextMenu(menuItems: {
                        //                                Button("清除此条历史", action: {
                        //                                    pasteRecord.clearPasteItem(item)
                        //                                })
                        //                            })
                        //                        )
                            .onTapGesture {
                                SystemPaste.writeToPaste(data: item)
                                AlertHistoryView.tryCloseThis()
                            }.help("单击复制到当前剪切板")
                            .focusable(true)
                        
                        Spacer()
                        Button(action: {
                            pasteRecord.clearPasteItem(item)
                        }, label: {
                            Text("X").foregroundColor(Color.black)
                        })
                            .help("清除此条历史")
                            .background(Color.red)
                            .cornerRadius(15)
                            
                    }
                    
                })
            })
            .navigationTitle("剪切板历史")
        }
        .padding()
        
    }
}

